# ----------------------------------------------------------------------------
# vectypes
# Copyright (c) 2009 Alex Holkner
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions 
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of vectypes nor the names of its
#    contributors may be used to endorse or promote products
#    derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

import optparse
import re

directive_pattern = re.compile(
    r'\s*#:\s*(.*)', flags=re.DOTALL)
begin_suite_pattern = re.compile(
    r'\s*#:\s*(def|if|elif|else|while|for|try).*', flags=re.DOTALL)
end_suite_pattern = re.compile(
    r'\s*#:\s*(else|elif|endif|enddef|endwhile|endfor|endtry)\s*', flags=re.DOTALL)
end_directives = (
    'else', 'endif', 'enddef', 'elif', 'endwhile', 'endfor', 'endtry')

template_pattern = re.compile(
    r'___([^_].*?)___')
def template_replace(match):
    return '" + str(%s) + "' % match.group(1)

def template(template_file, write_file, output_filename):
    indent = ''
    write_file.write('output_file = open("%s", "wt")\n' % output_filename)
    for line in template_file:
        match = directive_pattern.match(line)
        if match:
            # Directive
            if end_suite_pattern.match(line):
                indent = indent[:-4]

            directive = match.group(1)
            if directive.strip() not in end_directives:
                write_file.write(indent + directive)

            if begin_suite_pattern.match(line):
                indent += '    '
        
        else:
            # Templated output
            line = line.encode('string_escape')
            line = line.replace('"', r'\"')
            line = template_pattern.sub(template_replace, line)
            write_file.write('%soutput_file.write("%s")\n' % (indent, line))

if __name__ == '__main__':
    optparser = optparse.OptionParser()
    options, args = optparser.parse_args()
    template_filename, = args

    base_filename_pattern = re.compile('(.*).in.py')
    base_filename = base_filename_pattern.match(template_filename).group(1)
    write_filename = '%s.write.py' % base_filename
    output_filename = '%s.py' % base_filename

    template_file = open(template_filename, 'rt')
    write_file = open(write_filename, 'wt')
    template(template_file, write_file, output_filename)
